$(function(){
    var contentBox =$(".content")
  var page = 1;
  var totalPage = 0;
  var flag = false;
    function getProduct(page) {
        flag = true
    $.ajax({
            url:'http://192.168.0.181:8360/api/v4/product/pagination',
            method:'get',
            data:{
                page: page,
                 size: 10                
            },
            success:function(data){
                totalPage = data.data.totalPages;
                var productList = data.data.data
                productList.forEach(function (item, index) {
                  var str='<div class="box5 fl">'+
                  '<img src='+item.cover+' alt="">'+
                  '<h3>'+item.name+'</h3>'+
                  '<span>￥'+item.price+'</span>'+
                    '</div>'
                  contentBox.append($(str))
        
                })
                flag = false
            }
        })
    }
    getProduct(page)


  function tools(text,time){
    var t = $(".tools")
    t.html(text)
    t.css("display","block")
    setTimeout(function(){
      t.css("display","none")
    },time)
  }
  //下拉加载更多
  contentBox.scroll(function () {



    var contentBoxHeight = $(this).outerHeight();
    var scrollHeight = this.scrollHeight;
    var scrollTop = $(this).scrollTop();


    if(scrollTop >= 1500){
      $(".backTop").css("display","block")
    }else{
        $(".backTop").css("display","none")
    }


    if(flag){return}

    if (scrollHeight - contentBoxHeight <= scrollTop + 2) { 
      page++
      if(page > totalPage){
        return tools("没有更多数据",1500)
      }
      getProduct(page);
    }
  }) 


  //返回顶部
  $(".backTop").click(function(){
    var interId = setInterval(function(){
      var s= contentBox.scrollTop()
      contentBox.scrollTop(s-200) 
      if( contentBox.scrollTop() == 0){
        clearInterval(interId)
      }
    },30)}) 

  

})
/* <div class="box5 fl">
            <img src="http://imgservice.suning.cn/uimg1/b2c/image/SMYsXJ6nvKoazJoh487oJg.jpg_800w_800h_4e_100Q" alt="">
            <h3>苹果（Apple）iphoneX 移动联通电信智能手机 美版全新未激活有锁 完美解锁 深灰色/5.8</h3>
            <span>￥6739</span>
        </div> */